package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.SessionUtil;
import com.example.demo.config.Constant;
import com.example.demo.pojo.Article;
import com.example.demo.pojo.User;
import com.example.demo.service.ArticleService;
import com.example.demo.service.UserService;
import com.example.demo.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2023-01-07
 * Time: 11:21
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    /**
     * 获取用户的所有文章
     * @param username
     * @return
     */
    @RequestMapping("/getArticleByUser")
    public List<Article> getArticleByUser(String username){
        return articleService.getArticleByUser(username);
    }

    /**
     * 获取用户的所有文章
     * @param
     * @return
     */
    @RequestMapping("/mylist")
    public List<Article> getArticleList(HttpServletRequest request, String s){
        User userInfo = SessionUtil.getUserInfo(request);
        if( userInfo!=null){
            return articleService.getArticleList(userInfo.getId());
        }
        return null;
    }

    /**
     * 查看文章详情接口
     * @param aid 文章id
     * @return
     */
    @RequestMapping("/detail")
    public Object getArticleDetails(Integer aid){
        if( aid!=null && aid >0){
            return AjaxResult.success(articleService.getArticleDetails(aid));
        }
        return AjaxResult.fail(-1,"查询失败");

    }


    /**
     * 修改文章时 查看文章接口
     * @param aid 文章id
     * @return
     */
    @RequestMapping("/detailbyid")
    public Object getArticleDetailsById(HttpServletRequest request, Integer aid){
        //id的判断
        if( aid!=null && aid >0){
            //根据文章id获取文章信息
            Article articleInfo = articleService.getArticleDetails(aid);
            //文章归属人验证
            User userInfo = SessionUtil.getUserInfo(request);
            //文章归属人正确
            if( userInfo!=null && userInfo!=null && userInfo.getId() == articleInfo.getUid()){
                return AjaxResult.success(articleInfo);
            }
        }
        return AjaxResult.fail(-1,"查询失败");

    }

    /**
     * 修改文章接口
     * @param aid 文章id
     * @param title 文章标题
     * @param content 文章内容
     * @param request 请求
     * @return
     */
    @RequestMapping("/update")
    public Object updateArticle( Integer aid, String title, String content, HttpServletRequest request){
        if( aid!=null && aid>0){
            User userInfo = SessionUtil.getUserInfo(request);
            if( userInfo!=null){
                return articleService.updateArticle(aid, userInfo.getId(), title, content);
            }

        }
        return AjaxResult.fail(401, "修改失败");
    }

    /**
     * 删除文章接口
     * @param aid
     * @param request
     * @return
     */
    @RequestMapping("/delete")
    public Object daleteArticle( Integer aid, HttpServletRequest request){
        if( aid!=null && aid>0){
            User userInfo = SessionUtil.getUserInfo(request);
            if( userInfo!=null){
                return articleService.deleteArticle(aid);
            }
        }
        return AjaxResult.fail(401,"请先登录");
    }

    /**
     * 首页分页获取文章列表
     * @param pindex 页数
     * @param psize 每页的条数
     * @return
     */
    @RequestMapping("/list")
    public Object getList(Integer pindex, Integer psize){
        if( pindex==null || psize==null){
            return AjaxResult.fail(403,"参数不正确");
        }
        //计算偏移量
        Integer offset = (pindex-1)*psize;
        return AjaxResult.success(articleService.getList(psize,offset));
    }

    /**
     * 计算总共有多少页数据
     * @param psize 每页的条数
     * @return
     */
    @RequestMapping("/totalpage")
    public Object getTotalPage(Integer psize){
        if( psize == null || psize == 0){
            return AjaxResult.fail(403,"参数错误");
        }
        //获取总条数
        Integer count = articleService.getCount();
        int totalPage = (int) Math.ceil ((count*1.0)/psize);
        return totalPage;
    }

    @RequestMapping("/publish")
    public Object publishArticle(HttpServletRequest request, String title, String content){
        if( !StringUtils.hasLength(title) || !StringUtils.hasLength(content)){
            return AjaxResult.fail(403,"参数不全");
        }
        //获取当前登录用户的id
        Integer uid = SessionUtil.getUserInfo(request).getId();
        if(uid!=null) {
            return articleService.addArticle(uid, title, content);
        }
        return AjaxResult.fail(403,"用户未登录");

    }
}
